﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTelluro.Explorer.Infrastructure.CrossCutting.Logging;
using iTelluro.Explorer.Infrastructure.CrossCutting.NetFramework.Logging;
using Quartz;
using System.IO;

namespace SXMeteorological.DataSyn.Filedelete
{
    public class FiledeleteJob:IJob

    {
        private readonly ILogger _logger;

        public FiledeleteJob()
        {
            _logger = LoggerFrameFactory.CreateLog();
            _logger.LogInfo("执行文件删除构造函数");
        }

        public void Execute(IJobExecutionContext context)
        {
            Filedelete();
            leidadelete();
            xiazaidelete();
            Filedelete1();
        }

        private void Filedelete()
        {
            try
            {

                string[] filenames = Directory.GetDirectories(@"D:\Rending");
                DateTime dt = DateTime.Now;
                DateTime dt1 = DateTime.Now.AddDays(-2);
                DateTime dt2 = DateTime.Now.AddDays(-1);
                string datetime = Convert.ToDateTime(dt).ToString("yyyyMMdd") + "000000";
                string datetime1 = Convert.ToDateTime(dt1).ToString("yyyyMMdd") + "000000";
                string datetime2 = Convert.ToDateTime(dt2).ToString("yyyyMMdd") + "000000";
                foreach (string fname in filenames)
                {
                    string tt = fname.Remove(0, 11);
                    if(tt!=datetime&&tt!=datetime1&&tt!=datetime2)
                    {
                        Directory.Delete(fname, true);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(string.Format("删除文件失败#{0}：{1}", DateTime.Now, ex.Message));
            }
        }

        private void leidadelete()
        {
            try
            {
                string[] filenames = Directory.GetDirectories(@"D:\leida");
                DateTime dt = DateTime.Now;
                string datetime = Convert.ToDateTime(dt).ToString("yyyy-MM-dd");           
                foreach (string fname in filenames)
                {
                    string tt = fname.Remove(0, 9);
                    if (tt != datetime)
                    {
                        Directory.Delete(fname, true);
                    }
                }
            }
            catch(Exception ex)
            {
                _logger.LogError(string.Format("删除文件失败#{0}：{1}", DateTime.Now, ex.Message));
            }
        }

        private void xiazaidelete()
        {
            try
            {
                string[] filenames = Directory.GetDirectories(@"D:\xiazai");
                foreach (string fname in filenames)
                {
                        Directory.Delete(fname, true);                   
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(string.Format("删除文件失败#{0}：{1}", DateTime.Now, ex.Message));
            }
        }

        private void Filedelete1()
        {
            try
            {

                string[] filenames = Directory.GetDirectories(@"D:\RengionRending");
                DateTime dt = DateTime.Now;
                DateTime dt1 = DateTime.Now.AddDays(-2);
                DateTime dt2 = DateTime.Now.AddDays(-1);
                string datetime = Convert.ToDateTime(dt).ToString("yyyyMMdd") + "000000";
                string datetime1 = Convert.ToDateTime(dt1).ToString("yyyyMMdd") + "000000";
                string datetime2 = Convert.ToDateTime(dt2).ToString("yyyyMMdd") + "000000";
                foreach (string fname in filenames)
                {
                    string tt = fname.Remove(0, 11);
                    if (tt != datetime && tt != datetime1 && tt != datetime2)
                    {
                        Directory.Delete(fname, true);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(string.Format("删除文件失败#{0}：{1}", DateTime.Now, ex.Message));
            }
        }
    }
}
